package me.zhengjie.resources.controller;

import me.zhengjie.resources.utils.BeanMapUtil;
import me.zhengjie.resources.utils.SerialUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.Map;

//import cn.seenew.future.his.service.cache.util.RedisKeyHelper;

@RestController
@RequestMapping("/api/sso")
public class ResourceController {

    Logger logger = LoggerFactory.getLogger(ResourceController.class);


    @Autowired
    RedisConnectionFactory redisConnectionFactory;


    @RequestMapping(value = "/principal", method = RequestMethod.GET)
    public Map<String, ?> getUser(Principal principal) {
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>");
        logger.info(principal.toString());
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>");

        Map<String, Object> linkedMap = (LinkedHashMap<String, Object>) BeanMapUtil.beanToLinkedMap(principal);
        Object prinObject = linkedMap.get("principal");
        LinkedHashMap<String, Object> principalMap = new LinkedHashMap<>();
        principalMap.put("acId", "1");
        principalMap.put("principal", prinObject);
        linkedMap.put("principal", principalMap);
        OAuth2Authentication authentication = (OAuth2Authentication) principal;
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        String userCacheKey = "LOGIN:USER:INFO"; // RedisKeyHelper.getKeys(details.getTokenValue(), new Object[]{"LOGIN:USER:INFO"});
//        LoginInfo loginInfo = (new LogInfoService()).getLoginInfo();
//        redisConnectionFactory.getConnection().stringCommands().set(userCacheKey.getBytes(),  SerialUtils.serialize(loginInfo));
//        userAuth.getPrincipal()
        return linkedMap;
    }

}
